import pathToRegexp from 'path-to-regexp/index';
import {
    getEmplan,
    getYLBM,
    getAllRoleTow,
    getMaillist,
    postEmplan,
    deleteEmplan,
    putEmplan,
    getEmplanList,
    deleteEmplanList,
    postEmplanList,
    putEmplanList,
    postEmplanList_Third,
    deleteEmplanList_Third,
    putEmplanList_Third,
    postEmplanList_Tow,
    deleteEmplanList_Tow,
    putEmplanList_Tow,
    postWorkingEmp,
} from 'services/app';
import {message} from 'antd';

export default {
    namespace: 'emplan',
    state: {
        pages:1,
        record:{},
        arrValue:[{}],
        record_3:[{}],
    },
    subscriptions: {
        setupHistory ({ dispatch, history }) {
            history.listen((location) => {
                const match = pathToRegexp('/home').exec(location.pathname);
            });
        },
        setup ({ dispatch }) {},
    },
    effects: {
        //查看登录状态
        *getEmplan ({ payload }, { call, put }){
            const {data,total} = yield call(getEmplan, payload);
            yield put({ type: 'save', payload: {getEmplanData:data,total:total,pages:payload.pageNumber}});
        },
        //新增查询
        *newPost ({ payload }, { call, put }){
            const data = yield call(getAllRoleTow, {o_id:'222222'});
            const dataTow = yield call(getMaillist, {
                name:"",
                phone :"",
                pageNumber :0,
                pageSize :0,
            });
            yield put({ type: 'save', payload: {TXL:dataTow.data,BM:data.data}});
        },
        //新增预案
        *postEmplan ({ payload }, { call, put }){
            const data = yield call(postEmplan, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {pages:0,view:false}});
            }
        },
        //删除预案
        *deleteEmplan ({ payload }, { call, put }){
            const data = yield call(deleteEmplan, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {pages:0}});
            }
        },
        //修改预案
        *putEmplan ({ payload }, { call, put }){
            const data = yield call(putEmplan, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {pages:0,view:false}});
            }
        },
        //详情查询
        *goList ({ payload }, { call, put }){
            const {data} = yield call(getEmplanList, payload);
            const dataTow = yield call(getAllRoleTow, {o_id:'222222'});
            yield put({ type: 'save', payload: {ZRBM:dataTow.data,record_3:[{}],getEmergListData:data,visible:false,visible_1:false,visible_2:false}});
        },
        //启动预案
        *postWorking ({ payload }, { call, put }){
            const data = yield call(postWorkingEmp, payload);
            if(data.success){
                message.success('启动成功!');
                yield put({ type: 'save', payload: {
                    view_1:true,
                    arrOneView:false,
                }});
            }else {
                message.error('启动失败!');
            }
        },
        //岗位删除
        *listDelete ({ payload }, { call, put }){
            const data = yield call(deleteEmplanList, payload);
            if(data.code == 0){
                yield put({ type: 'goList', payload: {id:localStorage.getItem('id')}});
            }
        },
        //添加岗位信息
        *postList ({ payload }, { call, put }){
            const data = yield call(postEmplanList, payload);
            if(data.code == 0){
                yield put({ type: 'goList', payload: {id:localStorage.getItem('id')}});
            }
        },
        //修改岗位信息
        *putList ({ payload }, { call, put }){
            const data = yield call(putEmplanList, payload);
            if(data.code == 0){
                yield put({ type: 'goList', payload: {id:localStorage.getItem('id')}});
            }
        },
        //添加预案标准参数
        *postEmplanList_Tow ({ payload }, { call, put }){
            const data = yield call(postEmplanList_Tow, payload);
            if(data.code == 0){
                yield put({ type: 'goList', payload: {id:localStorage.getItem('id')}});
            }
        },
        //添加预案标准参数
        *deleteEmplanList_Tow ({ payload }, { call, put }){
            const data = yield call(deleteEmplanList_Tow, payload);
            if(data.code == 0){
                yield put({ type: 'goList', payload: {id:localStorage.getItem('id')}});
            }
        },
        //添加预案标准参数
        *putEmplanList_Tow ({ payload }, { call, put }){
            const data = yield call(putEmplanList_Tow, payload);
            if(data.code == 0){
                yield put({ type: 'goList', payload: {id:localStorage.getItem('id')}});
            }
        },
        //添加预案启动标准
        *postEmplanList_Third ({ payload }, { call, put }){
            const data = yield call(postEmplanList_Third, payload);
            if(data.code == 0){
                yield put({ type: 'goList', payload: {id:localStorage.getItem('id')}});
            }
        },
        //删除预案启动标准
        *deleteEmplanList_Third ({ payload }, { call, put }){
            const data = yield call(deleteEmplanList_Third, payload);
            if(data.code == 0){
                yield put({ type: 'goList', payload: {id:localStorage.getItem('id')}});
            }
        },
        //修改预案启动标准
        *putEmplanList_Third ({ payload }, { call, put }){
            const data = yield call(putEmplanList_Third, payload);
            if(data.code == 0){
                yield put({ type: 'goList', payload: {id:localStorage.getItem('id')}});
            }
        },
    },
    reducers: {
        save (state, { payload }) {
            return { ...state, ...payload };
        },
    },
};
